package gerenciaDados.cln.cgt.PI.classificacao;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Normalizacao {
	public static void zscore(double [][] mat,String [] rotulos,String saida) throws IOException
	{
		int nlinhas = mat.length;
		int ncolunas = mat[0].length;
		double med, med2,dvp;
		
		for(int j=0; j < ncolunas; j++)
		{
			med = 0.0;
			med2 = 0.0;
			for(int i=0; i<nlinhas; i++)
			{
				med += mat[i][j];
				med2 += mat[i][j]*mat[i][j];
			}
			
			med /= nlinhas;
			med2 /= nlinhas;
			dvp = Math.sqrt(med2 - med*med);			
			
			if(dvp != 0)
			{
				for(int i=0; i<nlinhas; i++)
				{
					mat[i][j] = (mat[i][j] - med)/dvp;
					
				}
			}
		}
		
		PrintWriter output = new PrintWriter(new FileWriter(saida.replace(".txt", "")  + "-normalizadozscore.txt"));		
		for(int i=0; i<nlinhas; i++)
		{			
			for(int j=0; j < ncolunas; j++)
			{
				output.print(mat[i][j] + ",");
			}
			output.println(rotulos[i]);			
		}		
		output.close();	
	}
	
	public static void linear(double [][] mat,String [] rotulos, String saida) throws IOException
	{
		int nlinhas = mat.length;
		int ncolunas = mat[0].length;
		double max = 0.0, min = 0.0;
		
		for(int j=0; j < ncolunas; j++)
		{
			max = -999999.0;
			min = 999999.0;
			
			for(int i=0; i<nlinhas; i++)
			{				
				if(mat[i][j] < min)
				{					
					min = mat[i][j];
				}				
				if(mat[i][j] > max)
				{
					max = mat[i][j];
				}
			}
			
			if(max - min != 0)
			{
				for(int i=0; i<nlinhas; i++)
				{
					mat[i][j] = (mat[i][j] - min)/(max - min);
					
				}
			}
		}
		
		PrintWriter output = new PrintWriter(new FileWriter(saida.replace(".txt", "")  + "-normalizadolinear.txt"));		
		for(int i=0; i<nlinhas; i++)
		{			
			for(int j=0; j < ncolunas; j++)
			{
				output.print(mat[i][j] + ",");				
			}
			output.println(rotulos[i]);				
		}		
		output.close();		
	}
}
